package engine.logic.loading.effect
{
	import com.greensock.TweenLite;
	
	import flash.display.Shape;
	import flash.events.Event;
	
	import newx.StageInfo;

	/**
	 *透明渐变特效 
	 * @author test
	 * 
	 */	
	public class Fade2 extends AbstractTransitionEffect
	{
		public function Fade2()
		{
			super();
			init();
		}
		
		private function init():void
		{
			_mask  = new Shape();
			_mask.graphics.beginFill(0x000000);
			_mask.graphics.drawRect(0,0,10,10);
			_mask.graphics.endFill();
			
			addChild(_mask);
		}
		
		private static const EffectInOver:int = 0;
		private static const EffectOutOver:int = 1;
		private var _type:int;
		
		private var _mask:Shape;
		private var _speed:Number = 0.1;
		
		private var _w:Number;
		private var _h:Number;
		
		override public function set width(value:Number):void
		{
			_w = value;
			_mask.width = _w;
		}
		
		override public function set height(value:Number):void
		{
			_h = value;
			_mask.height = _h;
		}
		
		/**
		 *过渡动画进入 
		 * 播完后派发EffectInComplete事件
		 */		
		override public function effectIn():void
		{
			_type = EffectInOver;
			_w = StageInfo.stage.stageWidth;
			_h = StageInfo.stage.stageHeight;
			
			_mask.width = _w;
			_mask.height= _h;
			_mask.alpha = 0;
			
			addEventListener(Event.ENTER_FRAME, update);
		}
		
		/**
		 *过渡动画移除 
		 * 播完后派发EffectOutComplete事件
		 */		
		override public function effectOut():void
		{
			_type = EffectOutOver;
			_w = StageInfo.stage.stageWidth;
			_h = StageInfo.stage.stageHeight;
			
			_mask.width = _w;
			_mask.height= _h;
			_mask.alpha = 1;
			
			addEventListener(Event.ENTER_FRAME, update);
		}
		
		private function update(e:Event):void
		{
			if(_type == EffectInOver)
			{
				_mask.alpha += _speed;
				
				if(_mask.alpha >= 1)
				{
					removeEventListener(Event.ENTER_FRAME,update);
					dispatchEvent(new TransitionEffectEvent(TransitionEffectEvent.EffectInOver));
				}
				
			}
			else
			{
				_mask.alpha -= _speed;
				
				if(_mask.alpha <= 0)
				{
					removeEventListener(Event.ENTER_FRAME,update);
					dispatchEvent(new TransitionEffectEvent(TransitionEffectEvent.EffectOutOver));
				}
			}
		}
	}
}